package com.xm.bean;

import org.apache.commons.lang3.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Element {


    private String type;//object array string int num...

    private String description;

    private Map<String, Element> properties;

    private Element items;

    private List<String> required;

    private boolean isOptional;//是否必填

    private int level = 0;

    private String fileName;



    public List<Element> floatElement(Element parentElement) {
        List<Element> elements = new ArrayList<>();
        if ("array".equals(this.type)) {
            Element items = this.getItems();
            String type = items.type;
            items.setLevel(parentElement.getLevel()+1);
            if ("array".equals(type)||"object".equals(type)){
                elements.add(items);
            }
            List<Element> resultElements = items.floatElement(items);
            if (resultElements.size()>0){
                elements.addAll(resultElements);
            }

        } else if ("object".equals(this.type)) {
            Map<String, Element> properties = this.getProperties();
            for (Map.Entry<String, Element> entry : properties.entrySet()) {
                String key = entry.getKey();
                Element value = entry.getValue();
                value.setFileName(key);
                String type = value.getType();
                value.setLevel(parentElement.getLevel()+1);
                List<String> required = parentElement.getRequired();
                if (null != required && required.contains(key)){
                    value.setOptional(true);
                }
                if ("array".equals(type)||"object".equals(type)){
                    elements.add(value);
                }
                List<Element> resultElements = value.floatElement(value);
                if (resultElements.size()>0){
                    elements.addAll(resultElements);
                }
            }
        } else {
            List<String> required = parentElement.getRequired();
            if (null != required && required.contains(this.getFileName())){
                this.setOptional(true);
            }
            elements.add(this);
            return elements;
        }
        return elements;
    }


    public boolean isOptional() {
        return isOptional;
    }

    public void setOptional(boolean optional) {
        isOptional = optional;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    private void doObj(Map<String, Element> properties) {

    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Map<String, Element> getProperties() {
        return properties;
    }

    public void setProperties(Map<String, Element> properties) {
        this.properties = properties;
    }

    public Element getItems() {
        return items;
    }

    public void setItems(Element items) {
        this.items = items;
    }

    public List<String> getRequired() {
        return required;
    }

    public void setRequired(List<String> required) {
        this.required = required;
    }

}
